 
#include "Pch.h"
#include "ScriptBind_Entity.h"
#include "Client/CoreApp.h"
#include "Client/OgRoot.h"

//------------------------------------------------------------------------
CScriptBind_Entity::CScriptBind_Entity( Ci::IScriptSystem * pScriptSystem, Ci::CCoreApp * pSystem )
{
	CScriptableBase::Init(pScriptSystem,pSystem);
	SetGlobalName("Entity");
 

     #undef SCRIPT_REG_CLASSNAME 
     #define SCRIPT_REG_CLASSNAME &CScriptBind_Entity::

	 SCRIPT_REG_FUNC(createEntity);
	 //SCRIPT_REG_FUNC(Detach);
	 SCRIPT_REG_FUNC(destoryEntity);
	 SCRIPT_REG_FUNC(setMaterialName);
	 SCRIPT_REG_FUNC(Attach);
	 SCRIPT_REG_FUNC(Detach);
}
//-----------------------------------------------------------------------------
CScriptBind_Entity::~CScriptBind_Entity()
{
}
 
//-----------------------------------------------------------------------------
int CScriptBind_Entity::createEntity(IFunctionHandler *pH)
{
	{
		const char* name;
		const char* meshname;
		 
		if(!pH->GetParam(1,name))
			m_pSS->RaiseError( "<CScriptBind_Entity::createEntity> parameter 1 not specified or nil (entity id to attach to)" );

		if(!pH->GetParam(2,meshname))
			m_pSS->RaiseError( "<CScriptBind_Entity::createEntity> parameter 2 not specified or nil (entity id to attach to)" );

		Ogre::Entity* ent = Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->createEntity(name,meshname);
	}

	return pH->EndFunction();
}

//-----------------------------------------------------------------------------
int CScriptBind_Entity::destoryEntity(IFunctionHandler *pH)
{

	const char* name;
	if(!pH->GetParam(1,name))
		m_pSS->RaiseError( "<CScriptBind_Entity::destoryEntity> parameter 1 not specified or nil (entity id to attach to)" );


	  Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->destroyEntity(name);

	return pH->EndFunction();
}
//-----------------------------------------------------------------------------
int CScriptBind_Entity::setMaterialName(IFunctionHandler *pH)
{
	const char* name;
	if(!pH->GetParam(1,name))
		m_pSS->RaiseError( "<CScriptBind_Entity::destoryEntity> parameter 1 not specified or nil (entity id to attach to)" );
	Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->getEntity(name)->setMaterialName(name);
	return pH->EndFunction();
}
//-----------------------------------------------------------------------------
int CScriptBind_Entity::Attach(IFunctionHandler * pH)
{

	const char* entname;
	const char* nodename;
	if(!pH->GetParam(1,entname))
		m_pSS->RaiseError( "<CScriptBind_Entity::Attach> parameter 1 not specified or nil (entity id to attach to)" );
	
	if(!pH->GetParam(2,nodename))
		m_pSS->RaiseError( "<CScriptBind_Entity::Attach> parameter 2 not specified or nil (entity id to attach to)" );

	Ogre::Entity* ent = Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->getEntity(entname);
	Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->getSceneNode(nodename)->attachObject(ent);

	return pH->EndFunction();
}
//-----------------------------------------------------------------------------
//		Detaches an effect.
int CScriptBind_Entity::Detach(IFunctionHandler * pH)
{
	const char* entname;
	const char* nodename;
	if(!pH->GetParam(1,entname))
		m_pSS->RaiseError( "<CScriptBind_Entity::Attach> parameter 1 not specified or nil (entity id to attach to)" );

	if(!pH->GetParam(2,nodename))
		m_pSS->RaiseError( "<CScriptBind_Entity::Attach> parameter 2 not specified or nil (entity id to attach to)" );

	Ogre::Entity* ent = Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->getEntity(entname);
	Ci::CCoreApp::getSingleton().getOgRoot()->getSceneMgr()->getSceneNode(nodename)->detachObject(ent);

	return pH->EndFunction();
}

//-----------------------------------------------------------------------------